(define brl-fill_jasperreport
  (lambda (report-file parameters conn ctxt)
    (let ((paras (make <java.util.HashMap>))
	  (session-key (static-field <net.sf.jasperreports.j2ee.servlets.BaseHttpServlet> 'DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE))
	  (http-session :: <javax.servlet.http.HttpSession>
		      (invoke
		       (as <javax.servlet.http.HttpServletRequest>
			   (brl-context-sv-req ctxt))
		       'getSession))
	  )  

       (for-each (lambda (key)
		   (let ((pname (make <java.lang.String> key)))
		     (invoke paras 'put pname (brl-hash-get parameters key))))
		 (brl-hash-keys parameters))      
;      (brl reportFile)
      (let ((jasper-print :: <net.sf.jasperreports.engine.JasperPrint> 
	     (invoke-static <net.sf.jasperreports.engine.JasperFillManager> 'fillReport
			    report-file
			    paras
			    conn)))
	(invoke http-session 'setAttribute session-key jasper-print)
	))))

 
(define brl-fill-jasperreport-and-export-to-image
   (lambda (report-file parameters image-file conn ctxt)
     (let ((paras (make <java.util.HashMap>))
	   (ostream (make <java.io.FileOutputStream> image-file))
	   )  
       (for-each (lambda (key)
		   (let ((pname (make <java.lang.String> key)))
		     (invoke paras 'put pname (brl-hash-get parameters key))))
		 (brl-hash-keys parameters))      
       (let* ((jasper-print :: <net.sf.jasperreports.engine.JasperPrint> 
			    (invoke-static <net.sf.jasperreports.engine.JasperFillManager> 'fillReport
					   report-file
					   paras
					   conn))
	      (fill-pages :: <java.util.List> 
			  (invoke jasper-print 'getPages))
	      (no-page (invoke fill-pages 'isEmpty))
	      (has-part (invoke jasper-print 'hasParts))
	      )
	 (if (and (not no-page) #t)
	     (let ( (buffImage :: <java.awt.image.BufferedImage>
			 (invoke-static <net.sf.jasperreports.engine.JasperPrintManager> 'printPageToImage 
					jasper-print 0 1.6))
		    )
	       (invoke-static <javax.imageio.ImageIO> 'write buffImage "jpg" ostream)))
	 )
       (invoke ostream 'close)
       )))  

(define hashListFromTable
  (lambda (st table-name fields filter-str . limit-number)

    (let ((hash-list '())
	  (res (sql-statement-results 
		st 
		(brl-string "select "
			    " top " (if (null? limit-number) 2000 (car limit-number))
			    (list-to-pure-comma-string fields)
			    " from " table-name
			    " where " filter-str
			    )
		)))
      (for-each
       (lambda (record)
	 (let ((my-hash (brl-hash)) (idx 0))
	   (for-each
	    (lambda (field)
	      (brl-hash-put! my-hash field (let ((val (list-ref record idx)))
					     (if (number? val) val (brl-string val))))
	      (set! idx (+ idx 1))) fields)
	   (set! hash-list (append hash-list (list my-hash)))
	   )) res)
      hash-list )))

(define jsonFromTable
  (lambda (st table-name fields filter-str . limit-number)

    (let ((q (open-output-string)))
     
      (json-write  (if (null?  limit-number)
		       (hashListFromTable st table-name fields filter-str )
		       (hashListFromTable st table-name fields filter-str (car limit-number)))
		   q)
      (get-output-string q)   
      )))
